var layutils={
    ajaxJsonPost:function (url,data,callback,errorback){
        $.ajax({
            url: url, // 目标 URL
            type: 'POST',
            data: data,
            contentType: 'application/json', // 不设置 Content-Type
            processData: false, // 不处理数据
            success: function (response) {
                callback(response);
            },
            error: function (xhr, status, error) {
                errorback(error);
            }
        });
    },
    ajaxGet:function (url,data,callback,errorback){
        $.ajax({
            url: url, // 目标 URL
            type: 'GET',
            data: data,
            processData: false, // 不处理数据
            success: function (response) {
                callback(response);
            },
            error: function (xhr, status, error) {
                errorback(error);
            }
        });
    },
    ajaxGetAwait:function (url,data){
        let resp=null;
        $.ajax({
            url: url, // 目标 URL
            type: 'GET',
            data: data,
            async: false, // 同步请求
            processData: false, // 不处理数据
            success: function (response) {
                resp=response;
            },
            error: function (xhr, status, error) {
                resp=error;
            }
        });
        return resp;
    },
    //给tree结构数据添加属性
    addPropertyTreeData:function (node,children, propertyName, propertyValue) {
        for (let i = 0; i < node.length; i++) {
            let obj=node[i];
            // 添加属性到当前节点
            obj[propertyName] = propertyValue;
            // 如果有子节点，递归调用
            if (obj[children] && obj[children].length > 0) {
                layutils.addPropertyTreeData(obj[children],children, propertyName, propertyValue);
            }
        }
    },
    //获取最深层的子元素
    getLeafNodes:function (nodes){
        let leafNodes = [];
        // 遍历当前节点数组
        for (let node of nodes) {
            // 如果没有子节点，添加到叶子节点数组中
            if (!node.children || node.children.length === 0) {
                leafNodes.push(node);
            } else {
                // 否则，递归处理子节点
                leafNodes = leafNodes.concat(layutils.getLeafNodes(node.children));
            }
        }
        return leafNodes;
    },
    objHasOwnProperty:function (obj,property,defalutValue ){
        if (!obj.hasOwnProperty(property)) {
            // 如果属性不存在，则添加
            obj[property] = defalutValue;
        }
    },
    objNullSetDefalutValue:function (obj,defalutValue){
        for (const key in obj) {
            if (obj[key] === null) {
                obj[key] = defalutValue;  // 将值为 null 的属性设置为空字符串
            }
        }
    },
    searchObjList:function (arr,field,searchValue){
        if(arr==undefined || arr==null || arr.length==0){
            return null;
        }
        for (let i = 0; i < arr.length ; i++) {
            let obj=arr[i];
            if(obj[field]==searchValue){
                return obj;
            }
        }
        return null;
    },
    addKeyIfNotExists:function (obj, key, callbak){
        obj[key] = obj[key] ?? callbak(key); // 如果 obj[key] 为 null 或 undefined，则赋值为 value
        return obj[key];
    },
    reloadScript:function (src,callback){
        // 找到所有 script 标签
        const scripts = document.querySelectorAll('script');
        scripts.forEach(script => {
            // 找到指定 src 的脚本
            if (script.src === src) {
                // 移除旧的脚本
                script.remove();
            }
        });
        // 重新加载脚本
        this.loadScript(src, function() {
            callback()
        });
    },
    loadScript:function (src, callback){
        const script = document.createElement('script');
        script.src = src;
        script.type = 'text/javascript';
        script.onload = callback; // 脚本加载完成后执行回调
        document.head.appendChild(script);
    },
    renderSelectFrom:function (name,url,value){
        let data=this.ajaxGetAwait(url,null);
        let str='';
        if(data.code===200){
            let list=data.data;
            str+='<select name="'+name+'" lay-filter="'+name+'" >';
            str+='<option value="">请选择</option>';
            for (let i = 0; i < list.length; i++) {
                let obj=list[i];
                if(value){
                    if(obj.value==value){
                        str+='<option value="'+obj.value+'" selected>'+obj.label+'</option>'
                    }else {
                        str+='<option value="'+obj.value+'">'+obj.label+'</option>'
                    }
                }else {
                    str+='<option value="'+obj.value+'">'+obj.label+'</option>';
                }
            }
            str+='</select>'
        }
        return str;
    }
}
const CookieUtil = {
    // 设置 Cookie
    set(name, value) {
        const date = new Date();
        date.setTime(date.getTime() + (  30 * 60 * 1000));
        document.cookie = `${name}=${value}; expires=${date.toUTCString()}; path=/`;
    },

    // 获取 Cookie
    get(name) {
        const cookies = document.cookie.split(';');
        for (let cookie of cookies) {
            const [cookieName, cookieValue] = cookie.trim().split('=');
            if (cookieName === name) {
                return cookieValue;
            }
        }
        return null;
    },

    // 删除 Cookie
    remove(name, path = "/") {
        document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path}`;
    }
};